//
//  PostListView.swift
//  WeiboDemo
//
//  Created by 姬向阳 on 2020/4/19.
//  Copyright © 2020 SUNNYJ. All rights reserved.
//

import SwiftUI
struct PostListView: View {
    let category : PostListCategory
    
    @EnvironmentObject var userData : UserData
    
    /* 用ZStack将PostCell与一个放在NavigationLink里的EmptyView重叠摆放,
     隐藏EmptyView只显示PostCell,以达到隐藏NavigationLink默认Cell附带链接三角效果
    */
    var body: some View {
        List {
            ForEach (userData.postList(for: category).list) { post in
                ZStack {
                    PostCell(post: post)
                    NavigationLink(destination: PostDetailView(post: post)) {
                        EmptyView()
                    }
                    .hidden()
                }
                // 去除在List中所有View的右移,必须与ForEach配合使用
                .listRowInsets(EdgeInsets())
            }
        }
    }
}

struct PostListView_Previews: PreviewProvider {
    static var previews: some View {
        NavigationView{
            PostListView(category: .recommend)
                .navigationBarTitle("返回")
                .navigationBarHidden(true)
        }
    .environmentObject(UserData())
    }
}
